//
//  XFXSportOneTableViewCell.m
//  XFXnews
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXSportOneTableViewCell.h"
#import "XFXNewsImageModel.h"

@interface XFXSportOneTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray *allArray;
@property (nonatomic, retain) UILabel *title;
@property (nonatomic, retain) UILabel *replyCount;
@property (nonatomic, retain) UICollectionView *myCollectionView;

@end

@implementation XFXSportOneTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }return self;
}

- (void)createSubViews{
    
    self.title = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH * 0.01, XFXHEIGHT * 0.016, XFXWIDTH, 10 * XFXSHEI)];
    self.title.font = [UIFont systemFontOfSize:13.0];
    [self.contentView addSubview:self.title];
    
    self.replyCount = [[UILabel alloc] initWithFrame:CGRectMake(XFXWIDTH - 85 * XFXSWID, XFXHEIGHT * 0.016, XFXWIDTH, 10 * XFXSHEI)];
    self.replyCount.font = [UIFont systemFontOfSize:14.0];
    self.replyCount.text = @"333";
    [self.contentView addSubview:self.replyCount];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((XFXWIDTH - 50) / 3, XFXHEIGHT * 0.13);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10 * XFXSHEI, 10 * XFXSWID, 10 * XFXSHEI);
    flowLayout.minimumLineSpacing = 10;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, self.title.frame.origin.y +self.title.frame.size.height + 5 * XFXSHEI, XFXWIDTH, XFXHEIGHT * 0.15) collectionViewLayout:flowLayout];
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.contentView addSubview:self.myCollectionView];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.allArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (XFXWIDTH - 40) / 3 * XFXSWID, 100 * XFXSHEI)];
    imageView.backgroundColor = [UIColor redColor];
    [cell.contentView addSubview:imageView];
    NSURL *url = [NSURL URLWithString:[self.allArray objectAtIndex:indexPath.row]];
    [imageView sd_setImageWithURL:url placeholderImage:nil];
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.myDelegate pushDetail:self.model];
}

-(void)setModel:(XFXNewsHeadLineModel *)model{
    
    _model = model;
    self.title.text = _model.title;
    
    self.replyCount.text = [NSString stringWithFormat:@"%@跟帖",_model.replyCount];
    
    self.allArray = [NSMutableArray array];
    for (NSDictionary *dic in _model.imgextra) {
        XFXNewsImageModel *model = [[XFXNewsImageModel alloc] init];
        
        [model setValuesForKeysWithDictionary:dic];
        
        [self.allArray addObject:model.imgsrc];
    }
    [self.allArray addObject:_model.imgsrc];
    [self.myCollectionView reloadData];
}


@end
